package com.fastfood.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fastfood.model.dto.Account;
import com.fastfood.model.dto.Role;

public class AccountDAO {

	public boolean addAccount(Connection conn, Account account) {
		String query = "insert into accounts (username, password, role_id, active) values (?, ?, ?, ?)";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, account.getUsername());
			preparedStatement.setString(2, account.getPassword());
			preparedStatement.setInt(3, account.getRole().getId());
			preparedStatement.setInt(4, account.getActive());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean deleteAccount(Connection conn, Account account) {
		String query = "delete from accounts where id = ?";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setInt(1, account.getId());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updateAccount(Connection conn, Account account) {
		String query = "update accounts set username = ?, password = ?, role_id = ?, active = ? where id = ?";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, account.getUsername());
			preparedStatement.setString(2, account.getPassword());
			preparedStatement.setInt(3, account.getRole().getId());
			preparedStatement.setInt(4, account.getActive());
			preparedStatement.setInt(5, account.getId());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Account getAccountById(Connection conn, int id) {
		Account account = null;
		try {
			ResultSet result = conn.createStatement().executeQuery("select * from accounts where id = " + id);
			if (result.next()) {
				RoleDAO dao = new RoleDAO();
				Role temp = dao.getRoleById(conn, result.getInt("role_id"));
				
				if (temp != null) {
					account = new Account();
					account.setId(result.getInt("id"));
					account.setUsername(result.getString("username"));
					account.setPassword(result.getString("password"));
					account.setRole(temp);
					account.setActive(result.getInt("active"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return account;
	}
	
	public Account getAccountByUsernameAndPassword(Connection conn, String username, String password) {
		String query = "select * from accounts where username = ? and password = ?";
		Account account = null;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, password);
			ResultSet result = preparedStatement.executeQuery();
			if (result.next()) {
				RoleDAO dao = new RoleDAO();
				Role temp = dao.getRoleById(conn, result.getInt("role_id"));
				
				if (temp != null) {
					account = new Account();
					account.setId(result.getInt("id"));
					account.setUsername(result.getString("username"));
					account.setPassword(result.getString("password"));
					account.setRole(temp);
					account.setActive(result.getInt("active"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return account;
	}
	
	public List<Account> getAllAccounts(Connection conn) {
		List<Account> accounts = null;
		try {
			ResultSet result = conn.createStatement().executeQuery("select * from accounts");
			accounts = new ArrayList<Account>();
			while (result.next()) {
				Account account = new Account();
				RoleDAO dao = new RoleDAO();
				Role temp = dao.getRoleById(conn, result.getInt("role_id"));
				
				if (temp != null) {
					account = new Account();
					account.setId(result.getInt("id"));
					account.setUsername(result.getString("username"));
					account.setPassword(result.getString("password"));
					account.setRole(temp);
					account.setActive(result.getInt("active"));
					
					accounts.add(account);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return accounts;
	}
}
